﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////


#include "IProjectConfigManager.h"
#include "GmepProductConsumeDataProvider.h"
#include "UiDocumentViewUtils.h"
#include "IDocument.h"
#include "GmepICollaborationProjectConfigs.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

using namespace gcs;
using namespace gcmp;
using namespace gmep;
using namespace gap;


std::wstring gcs::GmepProductConsumeDataProvider::AreaNameHint(EGcsEdition edition) const
{
    std::wstring  areaName;
    gcmp::IDocument* pDoc = gcmp::UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_UNLESS(pDoc, areaName, L"pDoc 为空", L"GMEP", L"2024-01-30");
    
    const ICollaborationProjectConfigs* pConstProjectConfigs = ICollaborationProjectConfigs::Get(pDoc);
    if (pConstProjectConfigs == nullptr)
    {
        return areaName;
    }
    const std::wstring buildingId = pConstProjectConfigs->GetBuildingId();
    gap::IProjectConfigManager* pProjectConfigManager = gap::IProjectConfigManager::Get(pDoc);
    DBG_WARN_AND_RETURN_UNLESS(pProjectConfigManager, areaName, L"pProjectConfigManager 为空", L"GMEP", L"2024-01-30");

    gap::ProjectInformation projectInfomation;
    if (!pProjectConfigManager->GetProjectInformationByBuildingItemId(buildingId, projectInfomation))
    {
        DBG_WARN(L"获取项目信息失败", L"GMEP", L"2024-01-30");
        return areaName;
    }

    //省
    std::wstring province;
    auto findIter = projectInfomation.Properties.find(ProvinceGdsCode);
    if (findIter != projectInfomation.Properties.end())
    {
        if (findIter->second.compatible(std::wstring()))
        {
            province = findIter->second.cast<std::wstring>();
        }                    
    }

    //市
    std::wstring city;
    auto findCityIter = projectInfomation.Properties.find(CityGdsCode);
    if (findCityIter != projectInfomation.Properties.end())
    {
        if (findCityIter->second.compatible(std::wstring()))
        {
            city = findCityIter->second.cast<std::wstring>();
        }
    }

    /*只有省名：传 省名
        只有市名：传 市名
        省市都有：传 省名 - 市名
        省市均无：传 空字符串*/
    if (!province.empty() && !city.empty())
    {
        areaName = province + L"-" + city;
    }
    else if(!province.empty() && city.empty())
    {
        areaName = province;
    }
    else if (!province.empty() && city.empty())
    {
        areaName = province;
    }
    return areaName;
}
